package binarySortTree;

import jdk.nashorn.internal.runtime.regexp.joni.SearchAlgorithm;

import java.util.prefs.BackingStoreException;

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        System.out.println("中序遍历");
        binarySortTree.infixOrder();
        System.out.println("删除叶子结点后");
        binarySortTree.delNode(1);
        binarySortTree.delNode(2);
        binarySortTree.delNode(3);
        binarySortTree.delNode(5);
        binarySortTree.delNode(7);
        binarySortTree.delNode(9);
        binarySortTree.delNode(12);
        binarySortTree.delNode(10);
        binarySortTree.infixOrder();
    }
}

//创建二叉排序树
class BinarySortTree {
    private Node root;

    public Node getRoot() {
        return root;
    }

    //查找要删除的结点
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    //查找要删除结点的父节点
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //删除结点
    public void delNode(int value) {
        if (root == null) {
            return;
        }
        //1.需要先去找到要删除的结点 targetNode
        Node targetNode = search(value);
        if (targetNode == null) {
            return;
        }
        //如果我们发现当前这颗二排序树只有一个结点
        if (root.left == null && root.right == null) {
            root = null;
            return;
        }

        //去找到targetNode的父节点
        Node parent = searchParent(value);

        //如果要删除的结点是叶子结点
        if (targetNode.left == null && targetNode.right == null) {
            //判断 targeNode 是父节点的左子节点还是右子结点
            if (parent.left != null && parent.left.valuel == value) {
                parent.left = null;
            } else if (parent.right != null && parent.right.valuel == value) {
                parent.right = null;
            }
        } else if (targetNode.left != null && targetNode.right != null) {
            //删除有两颗子树的结点
            int minVal = delRightTreeMin(targetNode.right);
            targetNode.valuel = minVal;

        } else {
            //删除只有一颗子树的结点
            if (targetNode.left != null) {
                if (parent != null) {
                    //如果targetNode 是 parent 的左子节点
                    if (parent.left.valuel == value) {
                        parent.left = targetNode.left;
                    } else {
                        parent.right = targetNode.left;
                    }
                } else {
                    root = targetNode.left;
                }
            } else {
                if (parent != null) {
                    if (parent.left.valuel == value) {
                        parent.left = targetNode.right;
                    } else {
                        parent.right = targetNode.right;
                    }
                }else{
                    root=targetNode.right;
                }
            }
        }
    }

    /**
     * @param node 传入的结点
     * @return 返回以node为跟结点的儿茶排序树的最小结点的值
     * 删除node为结点的二叉排序树的最小结点
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.valuel);
        return target.valuel;
    }

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空");
        }
    }
}


//创建Node结点
class Node {
    int valuel;
    Node left;
    Node right;

    public Node(int valuel) {
        this.valuel = valuel;
    }
    //查找要删除结点的父节点

    /**
     * @param valuel 要查找的结点的值
     * @return 返回的是删除的结点的父节点，如果没有就返回null
     */
    public Node searchParent(int valuel) {
        if ((this.left != null && this.left.valuel == valuel)
                || (this.right != null && this.right.valuel == valuel)) {
            return this;
        } else {
            if (valuel < this.valuel && this.left != null) {
                return this.left.searchParent(valuel);
            } else if (valuel >= this.valuel && this.right != null) {
                return this.right.searchParent(valuel);
            } else {
                return null;
            }
        }
    }


    //查找要删除的结点
    public Node search(int value) {
        if (value == this.valuel) {
            return this;
        } else if (value < this.valuel) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "valuel=" + valuel +
                '}';
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }


    //添加结点的方法
    //递归的形式添加结点，注意要满足二差排序树的要求
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.valuel < this.valuel) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }
}
